package views;

import presenters.AdminAccountPresenter;
import com.example.wheresmystuff.R;
import data.Account;
import data.AccountSetAsAdmin;
import data.DBInterfaceError;
import data.GetAccount;
import data.LockAccount;
import data.UserRemove;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * Activity for admins to edit accounts
 * 
 * @author Kelsey
 */
public class AdminAccountActivity extends Activity implements IFieldView {

	/** presenter for Admin Account */
	private AdminAccountPresenter myPresenter;

	/** account being edited */
	private Account account;

	private RadioButton radioNormal;
	private RadioButton radioAdmin;
	private RadioButton radioLocked;

	@Override
	/**
	 * This is called on the start of the activity
	 * @param Bundle - the saved Instance that this interfaces it
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_account);

		myPresenter = new AdminAccountPresenter(this);

		initForms();
	}

	/**
	 * setup forms to have correct initial state
	 */
	private void initForms() {
		radioNormal = (RadioButton) findViewById(R.id.radio0);
		radioAdmin = (RadioButton) findViewById(R.id.radio1);
		radioLocked = (RadioButton) findViewById(R.id.radio2);

		account = new Account();
		try {
			GetAccount getAccount = new GetAccount(this, getIntent()
					.getExtras().getString("account"));
			account = getAccount.getResult();
			if (account == null)
				account = new Account();

		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}

		TextView title = (TextView) findViewById(R.id.textView1);
		title.setText(account.getAccountName());

		if (account.getIsAdmin()) {
			radioNormal.setChecked(false);
			radioAdmin.setChecked(true);
			radioLocked.setChecked(false);
		} else {
			if (account.getIsLocked()) {
				radioNormal.setChecked(false);
				radioAdmin.setChecked(false);
				radioLocked.setChecked(true);
			} else {
				radioNormal.setChecked(true);
				radioAdmin.setChecked(false);
				radioLocked.setChecked(false);
			}
		}
	}

	/**
	 * Gets called when the menu is created
	 * 
	 * @param menu
	 *            - the menu that gets caleld
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.admin_account, menu);
		return true;
	}

	/**
	 * Gets called when the radio gets clicked
	 * 
	 * @param source
	 *            - the source of the click
	 */
	public void onRadioNormalClick(View source) {
		try {
			AccountSetAsAdmin setAdmin = new AccountSetAsAdmin(this, account,
					false);
			setAdmin.getResult();

			LockAccount lockAccount = new LockAccount(this, account, false);
			lockAccount.getResult();

		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}
	}

	/**
	 * Gets clicked when the admin radio gets clicked
	 * 
	 * @param source
	 *            - the source of the radioclick
	 */
	public void onRadioAdminClick(View source) {
		try {
			AccountSetAsAdmin setAdmin = new AccountSetAsAdmin(this, account,
					true);
			setAdmin.getResult();

			LockAccount lockAccount = new LockAccount(this, account, false);
			lockAccount.getResult();

		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}
	}

	/**
	 * On the radio locked clicked, this gets called
	 * 
	 * @param source
	 *            - the source of the click
	 */
	public void onRadioLockedClick(View source) {
		try {
			AccountSetAsAdmin setAdmin = new AccountSetAsAdmin(this, account,
					false);
			setAdmin.getResult();

			LockAccount lockAccount = new LockAccount(this, account, true);
			lockAccount.getResult();

		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}
	}

	/**
	 * Gets called when the onRemove button gets clicked
	 * 
	 * @param source
	 *            - when the onclick is clicked, this is the source of it
	 */
	public void onRemoveClick(View source) {
		try {
			UserRemove userRemove = new UserRemove(this, account);
			userRemove.getResult();

		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}

		Intent intent = new Intent(this, AdminListActivity.class);
		startActivity(intent);
		finish();
	}

	/**
	 * This gets clicked when the backclick gets called
	 * 
	 * @param source
	 *            - the source of the click
	 */
	public void onBackClick(View source) {
		myPresenter.onBackClick(source);
	}

	@Override
	public void advance(View source) {
		Intent intent;
		switch (source.getId()) {
		case R.id.button2: // back
			intent = new Intent(this, AdminListActivity.class);
			startActivity(intent);
			finish();
		}
	}

	@Override
	public void showToast(String toast) {
		// TODO Auto-generated method stub

	}

	@Override
	public void hideSoftKeyboard(View source) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showProgressBar() {
		// TODO Auto-generated method stub

	}

}
